# Prometheus主配置文件
# 此文件定义Prometheus的全局配置、抓取目标、告警规则等
# 配置参考：https://prometheus.io/docs/prometheus/latest/configuration/configuration/

# === 全局配置 ===
global:
  # 抓取间隔（默认1分钟）
  scrape_interval: {{ prometheus_global_scrape_interval }}
  
  # 规则评估间隔（默认1分钟）
  evaluation_interval: {{ prometheus_global_evaluation_interval }}
  
  # 外部标签（添加到所有时间序列和告警）
  external_labels:
    monitor: '{{ prometheus_external_labels.monitor | default("prometheus") }}'
    replica: '{{ prometheus_external_labels.replica | default(ansible_hostname) }}'
{% for key, value in prometheus_external_labels.items() %}
{% if key not in ['monitor', 'replica'] %}
    {{ key }}: '{{ value }}'
{% endif %}
{% endfor %}

# === 告警规则文件 ===
rule_files:
{% for rule_file in prometheus_rule_files %}
  - "{{ rule_file }}"
{% endfor %}

# === 告警管理器配置 ===
{% if prometheus_alertmanager_config.enabled %}
alerting:
  alertmanagers:
    - static_configs:
        - targets:
{% for target in prometheus_alertmanager_config.targets %}
          - "{{ target }}"
{% endfor %}
{% if prometheus_alertmanager_config.path_prefix %}
      path_prefix: "{{ prometheus_alertmanager_config.path_prefix }}"
{% endif %}
{% if prometheus_alertmanager_config.scheme %}
      scheme: "{{ prometheus_alertmanager_config.scheme }}"
{% endif %}
{% endif %}

# === 抓取配置 ===
scrape_configs:
{% for scrape_config in prometheus_scrape_configs %}
  # {{ scrape_config.job_name }} 任务配置
  - job_name: '{{ scrape_config.job_name }}'
    
    # 抓取间隔（覆盖全局设置）
{% if scrape_config.scrape_interval is defined %}
    scrape_interval: {{ scrape_config.scrape_interval }}
{% endif %}
    
    # 抓取超时时间
{% if scrape_config.scrape_timeout is defined %}
    scrape_timeout: {{ scrape_config.scrape_timeout }}
{% endif %}
    
    # 指标路径
{% if scrape_config.metrics_path is defined %}
    metrics_path: {{ scrape_config.metrics_path }}
{% endif %}
    
    # URL参数
{% if scrape_config.params is defined %}
    params:
{% for key, values in scrape_config.params.items() %}
      {{ key }}:
{% for value in values %}
        - '{{ value }}'
{% endfor %}
{% endfor %}
{% endif %}
    
    # 协议方案
{% if scrape_config.scheme is defined %}
    scheme: {{ scrape_config.scheme }}
{% endif %}
    
    # 基本认证
{% if scrape_config.basic_auth is defined %}
    basic_auth:
      username: '{{ scrape_config.basic_auth.username }}'
      password: '{{ scrape_config.basic_auth.password }}'
{% endif %}
    
    # TLS配置
{% if scrape_config.tls_config is defined %}
    tls_config:
{% if scrape_config.tls_config.ca_file is defined %}
      ca_file: '{{ scrape_config.tls_config.ca_file }}'
{% endif %}
{% if scrape_config.tls_config.cert_file is defined %}
      cert_file: '{{ scrape_config.tls_config.cert_file }}'
{% endif %}
{% if scrape_config.tls_config.key_file is defined %}
      key_file: '{{ scrape_config.tls_config.key_file }}'
{% endif %}
{% if scrape_config.tls_config.insecure_skip_verify is defined %}
      insecure_skip_verify: {{ scrape_config.tls_config.insecure_skip_verify | lower }}
{% endif %}
{% endif %}
    
    # 静态目标配置
{% if scrape_config.static_configs is defined %}
    static_configs:
{% for static_config in scrape_config.static_configs %}
      - targets:
{% for target in static_config.targets %}
          - '{{ target }}'
{% endfor %}
{% if static_config.labels is defined %}
        labels:
{% for key, value in static_config.labels.items() %}
          {{ key }}: '{{ value }}'
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
    
    # 文件服务发现
{% if scrape_config.file_sd_configs is defined %}
    file_sd_configs:
{% for file_sd_config in scrape_config.file_sd_configs %}
      - files:
{% for file in file_sd_config.files %}
          - '{{ file }}'
{% endfor %}
{% if file_sd_config.refresh_interval is defined %}
        refresh_interval: {{ file_sd_config.refresh_interval }}
{% endif %}
{% endfor %}
{% endif %}
    
    # 标签重写规则
{% if scrape_config.relabel_configs is defined %}
    relabel_configs:
{% for relabel_config in scrape_config.relabel_configs %}
      - source_labels:
{% for label in relabel_config.source_labels %}
          - {{ label }}
{% endfor %}
{% if relabel_config.separator is defined %}
        separator: '{{ relabel_config.separator }}'
{% endif %}
{% if relabel_config.target_label is defined %}
        target_label: {{ relabel_config.target_label }}
{% endif %}
{% if relabel_config.regex is defined %}
        regex: '{{ relabel_config.regex }}'
{% endif %}
{% if relabel_config.replacement is defined %}
        replacement: '{{ relabel_config.replacement }}'
{% endif %}
{% if relabel_config.action is defined %}
        action: {{ relabel_config.action }}
{% endif %}
{% endfor %}
{% endif %}
    
    # 指标重写规则
{% if scrape_config.metric_relabel_configs is defined %}
    metric_relabel_configs:
{% for metric_relabel_config in scrape_config.metric_relabel_configs %}
      - source_labels:
{% for label in metric_relabel_config.source_labels %}
          - {{ label }}
{% endfor %}
{% if metric_relabel_config.target_label is defined %}
        target_label: {{ metric_relabel_config.target_label }}
{% endif %}
{% if metric_relabel_config.regex is defined %}
        regex: '{{ metric_relabel_config.regex }}'
{% endif %}
{% if metric_relabel_config.replacement is defined %}
        replacement: '{{ metric_relabel_config.replacement }}'
{% endif %}
{% if metric_relabel_config.action is defined %}
        action: {{ metric_relabel_config.action }}
{% endif %}
{% endfor %}
{% endif %}

{% endfor %}

# === 远程写入配置 ===
{% if prometheus_remote_write is defined and prometheus_remote_write | length > 0 %}
remote_write:
{% for remote_write_config in prometheus_remote_write %}
  - url: "{{ remote_write_config.url }}"
{% if remote_write_config.name is defined %}
    name: "{{ remote_write_config.name }}"
{% endif %}
{% if remote_write_config.basic_auth is defined %}
    basic_auth:
      username: "{{ remote_write_config.basic_auth.username }}"
      password: "{{ remote_write_config.basic_auth.password }}"
{% endif %}
{% if remote_write_config.write_relabel_configs is defined %}
    write_relabel_configs:
{% for relabel_config in remote_write_config.write_relabel_configs %}
      - source_labels:
{% for label in relabel_config.source_labels %}
          - {{ label }}
{% endfor %}
        target_label: {{ relabel_config.target_label }}
        regex: '{{ relabel_config.regex }}'
        replacement: '{{ relabel_config.replacement }}'
        action: {{ relabel_config.action }}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}

# === 远程读取配置 ===
{% if prometheus_remote_read is defined and prometheus_remote_read | length > 0 %}
remote_read:
{% for remote_read_config in prometheus_remote_read %}
  - url: "{{ remote_read_config.url }}"
{% if remote_read_config.name is defined %}
    name: "{{ remote_read_config.name }}"
{% endif %}
{% if remote_read_config.basic_auth is defined %}
    basic_auth:
      username: "{{ remote_read_config.basic_auth.username }}"
      password: "{{ remote_read_config.basic_auth.password }}"
{% endif %}
{% endfor %}
{% endif %}